home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / gas / expr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  41.7 KB  |  1,623 lines

  1. /* expr.c -operands, expressions-
  2.    Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 1996
  3.    Free Software Foundation, Inc.
  4.  
  5.    This file is part of GAS, the GNU Assembler.
  6.  
  7.    GAS is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2, or (at your option)
  10.    any later version.
  11.  
  12.    GAS is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with GAS; see the file COPYING.  If not, write to
  19.    the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  20.  
  21. /*
  22.  * This is really a branch office of as-read.c. I split it out to clearly
  23.  * distinguish the world of expressions from the world of statements.
  24.  * (It also gives smaller files to re-compile.)
  25.  * Here, "operand"s are of expressions, not instructions.
  26.  */
  27.  
  28. #include <ctype.h>
  29. #include <string.h>
  30.  
  31. #include "as.h"
  32. #include "libiberty.h"
  33. #include "obstack.h"
  34.  
  35. static void floating_constant PARAMS ((expressionS * expressionP));
  36. static void integer_constant PARAMS ((int radix, expressionS * expressionP));
  37. static void mri_char_constant PARAMS ((expressionS *));
  38. static void current_location PARAMS ((expressionS *));
  39. static void clean_up_expression PARAMS ((expressionS * expressionP));
  40.  
  41. extern const char EXP_CHARS[], FLT_CHARS[];
  42.  
  43. /* We keep a mapping of expression symbols to file positions, so that
  44.    we can provide better error messages.  */
  45.  
  46. struct expr_symbol_line
  47. {
  48.   struct expr_symbol_line *next;
  49.   symbolS *sym;
  50.   char *file;
  51.   unsigned int line;
  52. };
  53.  
  54. static struct expr_symbol_line *expr_symbol_lines;
  55.  
  56. /* Build a dummy symbol to hold a complex expression.  This is how we
  57.    build expressions up out of other expressions.  The symbol is put
  58.    into the fake section expr_section.  */
  59.  
  60. symbolS *
  61. make_expr_symbol (expressionP)
  62.      expressionS *expressionP;
  63. {
  64.   const char *fake;
  65.   symbolS *symbolP;
  66.   struct expr_symbol_line *n;
  67.  
  68.   if (expressionP->X_op == O_symbol
  69.       && expressionP->X_add_number == 0)
  70.     return expressionP->X_add_symbol;
  71.  
  72.   /* FIXME: This should be something which decode_local_label_name
  73.      will handle.  */
  74.   fake = FAKE_LABEL_NAME;
  75.  
  76.   /* Putting constant symbols in absolute_section rather than
  77.      expr_section is convenient for the old a.out code, for which
  78.      S_GET_SEGMENT does not always retrieve the value put in by
  79.      S_SET_SEGMENT.  */
  80.   symbolP = symbol_create (fake,
  81.                (expressionP->X_op == O_constant
  82.                 ? absolute_section
  83.                 : expr_section),
  84.                0, &zero_address_frag);
  85.   symbolP->sy_value = *expressionP;
  86.  
  87.   if (expressionP->X_op == O_constant)
  88.     resolve_symbol_value (symbolP);
  89.  
  90.   n = (struct expr_symbol_line *) xmalloc (sizeof *n);
  91.   n->sym = symbolP;
  92.   as_where (&n->file, &n->line);
  93.   n->next = expr_symbol_lines;
  94.   expr_symbol_lines = n;
  95.  
  96.   return symbolP;
  97. }
  98.  
  99. /* Return the file and line number for an expr symbol.  Return
  100.    non-zero if something was found, 0 if no information is known for
  101.    the symbol.  */
  102.  
  103. int
  104. expr_symbol_where (sym, pfile, pline)
  105.      symbolS *sym;
  106.      char **pfile;
  107.      unsigned int *pline;
  108. {
  109.   register struct expr_symbol_line *l;
  110.  
  111.   for (l = expr_symbol_lines; l != NULL; l = l->next)
  112.     {
  113.       if (l->sym == sym)
  114.     {
  115.       *pfile = l->file;
  116.       *pline = l->line;
  117.       return 1;
  118.     }
  119.     }
  120.  
  121.   return 0;
  122. }
  123.  
  124. /*
  125.  * Build any floating-point literal here.
  126.  * Also build any bignum literal here.
  127.  */
  128.  
  129. /* Seems atof_machine can backscan through generic_bignum and hit whatever
  130.    happens to be loaded before it in memory.  And its way too complicated
  131.    for me to fix right.  Thus a hack.  JF:  Just make generic_bignum bigger,
  132.    and never write into the early words, thus they'll always be zero.
  133.    I hate Dean's floating-point code.  Bleh.  */
  134. LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
  135. FLONUM_TYPE generic_floating_point_number =
  136. {
  137.   &generic_bignum[6],        /* low (JF: Was 0) */
  138.   &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high JF: (added +6) */
  139.   0,                /* leader */
  140.   0,                /* exponent */
  141.   0                /* sign */
  142. };
  143. /* If nonzero, we've been asked to assemble nan, +inf or -inf */
  144. int generic_floating_point_magic;
  145.  
  146. static void
  147. floating_constant (expressionP)
  148.      expressionS *expressionP;
  149. {
  150.   /* input_line_pointer->*/
  151.   /* floating-point constant. */
  152.   int error_code;
  153.  
  154.   error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
  155.                  &generic_floating_point_number);
  156.  
  157.   if (error_code)
  158.     {
  159.       if (error_code == ERROR_EXPONENT_OVERFLOW)
  160.     {
  161.       as_bad ("bad floating-point constant: exponent overflow, probably assembling junk");
  162.     }
  163.       else
  164.     {
  165.       as_bad ("bad floating-point constant: unknown error code=%d.", error_code);
  166.     }
  167.     }
  168.   expressionP->X_op = O_big;
  169.   /* input_line_pointer->just after constant, */
  170.   /* which may point to whitespace. */
  171.   expressionP->X_add_number = -1;
  172. }
  173.  
  174. static void
  175. integer_constant (radix, expressionP)
  176.      int radix;
  177.      expressionS *expressionP;
  178. {
  179.   char *start;        /* start of number. */
  180.   char *suffix = NULL;
  181.   char c;
  182.   valueT number;    /* offset or (absolute) value */
  183.   short int digit;    /* value of next digit in current radix */
  184.   short int maxdig = 0;/* highest permitted digit value. */
  185.   int too_many_digits = 0;    /* if we see >= this number of */
  186.   char *name;        /* points to name of symbol */
  187.   symbolS *symbolP;    /* points to symbol */
  188.  
  189.   int small;            /* true if fits in 32 bits. */
  190.  
  191.   /* May be bignum, or may fit in 32 bits. */
  192.   /* Most numbers fit into 32 bits, and we want this case to be fast.
  193.      so we pretend it will fit into 32 bits.  If, after making up a 32
  194.      bit number, we realise that we have scanned more digits than
  195.      comfortably fit into 32 bits, we re-scan the digits coding them
  196.      into a bignum.  For decimal and octal numbers we are
  197.      conservative: Some numbers may be assumed bignums when in fact
  198.      they do fit into 32 bits.  Numbers of any radix can have excess
  199.      leading zeros: We strive to recognise this and cast them back
  200.      into 32 bits.  We must check that the bignum really is more than
  201.      32 bits, and change it back to a 32-bit number if it fits.  The
  202.      number we are looking for is expected to be positive, but if it
  203.      fits into 32 bits as an unsigned number, we let it be a 32-bit
  204.      number.  The cavalier approach is for speed in ordinary cases. */
  205.   /* This has been extended for 64 bits.  We blindly assume that if
  206.      you're compiling in 64-bit mode, the target is a 64-bit machine.
  207.      This should be cleaned up.  */
  208.  
  209. #ifdef BFD64
  210. #define valuesize 64
  211. #else /* includes non-bfd case, mostly */
  212. #define valuesize 32
  213. #endif
  214.  
  215.   if (flag_m68k_mri && radix == 0)
  216.     {
  217.       int flt = 0;
  218.  
  219.       /* In MRI mode, the number may have a suffix indicating the
  220.          radix.  For that matter, it might actually be a floating
  221.          point constant.  */
  222.       for (suffix = input_line_pointer; isalnum (*suffix); suffix++)
  223.     {
  224.       if (*suffix == 'e' || *suffix == 'E')
  225.         flt = 1;
  226.     }
  227.  
  228.       if (suffix == input_line_pointer)
  229.     {
  230.       radix = 10;
  231.       suffix = NULL;
  232.     }
  233.       else
  234.     {
  235.       c = *--suffix;
  236.       if (islower (c))
  237.         c = toupper (c);
  238.       if (c == 'B')
  239.         radix = 2;
  240.       else if (c == 'D')
  241.         radix = 10;
  242.       else if (c == 'O' || c == 'Q')
  243.         radix = 8;
  244.       else if (c == 'H')
  245.         radix = 16;
  246.       else if (suffix[1] == '.' || c == 'E' || flt)
  247.         {
  248.           floating_constant (expressionP);
  249.           return;
  250.         }
  251.       else
  252.         {
  253.           radix = 10;
  254.           suffix = NULL;
  255.         }
  256.     }
  257.     }
  258.  
  259.   switch (radix)
  260.     {
  261.     case 2:
  262.       maxdig = 2;
  263.       too_many_digits = valuesize + 1;
  264.       break;
  265.     case 8:
  266.       maxdig = radix = 8;
  267.       too_many_digits = (valuesize + 2) / 3 + 1;
  268.       break;
  269.     case 16:
  270.       maxdig = radix = 16;
  271.       too_many_digits = (valuesize + 3) / 4 + 1;
  272.       break;
  273.     case 10:
  274.       maxdig = radix = 10;
  275.       too_many_digits = (valuesize + 12) / 4; /* very rough */
  276.     }
  277. #undef valuesize
  278.   start = input_line_pointer;
  279.   c = *input_line_pointer++;
  280.   for (number = 0;
  281.        (digit = hex_value (c)) < maxdig;
  282.        c = *input_line_pointer++)
  283.     {
  284.       number = number * radix + digit;
  285.     }
  286.   /* c contains character after number. */
  287.   /* input_line_pointer->char after c. */
  288.   small = (input_line_pointer - start - 1) < too_many_digits;
  289.   if (!small)
  290.     {
  291.       /*
  292.        * we saw a lot of digits. manufacture a bignum the hard way.
  293.        */
  294.       LITTLENUM_TYPE *leader;    /*->high order littlenum of the bignum. */
  295.       LITTLENUM_TYPE *pointer;    /*->littlenum we are frobbing now. */
  296.       long carry;
  297.  
  298.       leader = generic_bignum;
  299.       generic_bignum[0] = 0;
  300.       generic_bignum[1] = 0;
  301.       input_line_pointer = start;    /*->1st digit. */
  302.       c = *input_line_pointer++;
  303.       for (;
  304.        (carry = hex_value (c)) < maxdig;
  305.        c = *input_line_pointer++)
  306.     {
  307.       for (pointer = generic_bignum;
  308.            pointer <= leader;
  309.            pointer++)
  310.         {
  311.           long work;
  312.  
  313.           work = carry + radix * *pointer;
  314.           *pointer = work & LITTLENUM_MASK;
  315.           carry = work >> LITTLENUM_NUMBER_OF_BITS;
  316.         }
  317.       if (carry)
  318.         {
  319.           if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
  320.         {
  321.           /* room to grow a longer bignum. */
  322.           *++leader = carry;
  323.         }
  324.         }
  325.     }
  326.       /* again, c is char after number, */
  327.       /* input_line_pointer->after c. */
  328.       know (LITTLENUM_NUMBER_OF_BITS == 16);
  329.       if (leader < generic_bignum + 2)
  330.     {
  331.       /* will fit into 32 bits. */
  332.       number =
  333.         ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
  334.         | (generic_bignum[0] & LITTLENUM_MASK);
  335.       small = 1;
  336.     }
  337.       else
  338.     {
  339.       number = leader - generic_bignum + 1;    /* number of littlenums in the bignum. */
  340.     }
  341.     }
  342.  
  343.   if (flag_m68k_mri && suffix != NULL && input_line_pointer - 1 == suffix)
  344.     c = *input_line_pointer++;
  345.  
  346.   if (small)
  347.     {
  348.       /*
  349.        * here with number, in correct radix. c is the next char.
  350.        * note that unlike un*x, we allow "011f" "0x9f" to
  351.        * both mean the same as the (conventional) "9f". this is simply easier
  352.        * than checking for strict canonical form. syntax sux!
  353.        */
  354.  
  355.       if (LOCAL_LABELS_FB && c == 'b')
  356.     {
  357.       /*
  358.        * backward ref to local label.
  359.        * because it is backward, expect it to be defined.
  360.        */
  361.       /* Construct a local label.  */
  362.       name = fb_label_name ((int) number, 0);
  363.  
  364.       /* seen before, or symbol is defined: ok */
  365.       symbolP = symbol_find (name);
  366.       if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
  367.         {
  368.           /* local labels are never absolute. don't waste time
  369.          checking absoluteness. */
  370.           know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
  371.  
  372.           expressionP->X_op = O_symbol;
  373.           expressionP->X_add_symbol = symbolP;
  374.         }
  375.       else
  376.         {
  377.           /* either not seen or not defined. */
  378.           /* @@ Should print out the original string instead of
  379.          the parsed number.  */
  380.           as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.",
  381.               (int) number);
  382.           expressionP->X_op = O_constant;
  383.         }
  384.  
  385.       expressionP->X_add_number = 0;
  386.     }            /* case 'b' */
  387.       else if (LOCAL_LABELS_FB && c == 'f')
  388.     {
  389.       /*
  390.        * forward reference. expect symbol to be undefined or
  391.        * unknown. undefined: seen it before. unknown: never seen
  392.        * it before.
  393.        * construct a local label name, then an undefined symbol.
  394.        * don't create a xseg frag for it: caller may do that.
  395.        * just return it as never seen before.
  396.        */
  397.       name = fb_label_name ((int) number, 1);
  398.       symbolP = symbol_find_or_make (name);
  399.       /* we have no need to check symbol properties. */
  400. #ifndef many_segments
  401.       /* since "know" puts its arg into a "string", we
  402.          can't have newlines in the argument.  */
  403.       know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
  404. #endif
  405.       expressionP->X_op = O_symbol;
  406.       expressionP->X_add_symbol = symbolP;
  407.       expressionP->X_add_number = 0;
  408.     }            /* case 'f' */
  409.       else if (LOCAL_LABELS_DOLLAR && c == '$')
  410.     {
  411.       /* If the dollar label is *currently* defined, then this is just
  412.          another reference to it.  If it is not *currently* defined,
  413.          then this is a fresh instantiation of that number, so create
  414.          it.  */
  415.  
  416.       if (dollar_label_defined ((long) number))
  417.         {
  418.           name = dollar_label_name ((long) number, 0);
  419.           symbolP = symbol_find (name);
  420.           know (symbolP != NULL);
  421.         }
  422.       else
  423.         {
  424.           name = dollar_label_name ((long) number, 1);
  425.           symbolP = symbol_find_or_make (name);
  426.         }
  427.  
  428.       expressionP->X_op = O_symbol;
  429.       expressionP->X_add_symbol = symbolP;
  430.       expressionP->X_add_number = 0;
  431.     }            /* case '$' */
  432.       else
  433.     {
  434.       expressionP->X_op = O_constant;
  435. #ifdef TARGET_WORD_SIZE
  436.       /* Sign extend NUMBER.  */
  437.       number |= (-(number >> (TARGET_WORD_SIZE - 1))) << (TARGET_WORD_SIZE - 1);
  438. #endif
  439.       expressionP->X_add_number = number;
  440.       input_line_pointer--;    /* restore following character. */
  441.     }            /* really just a number */
  442.     }
  443.   else
  444.     {
  445.       /* not a small number */
  446.       expressionP->X_op = O_big;
  447.       expressionP->X_add_number = number;    /* number of littlenums */
  448.       input_line_pointer--;    /*->char following number. */
  449.     }
  450. }
  451.  
  452. /* Parse an MRI multi character constant.  */
  453.  
  454. static void
  455. mri_char_constant (expressionP)
  456.      expressionS *expressionP;
  457. {
  458.   int i;
  459.  
  460.   if (*input_line_pointer == '\''
  461.       && input_line_pointer[1] != '\'')
  462.     {
  463.       expressionP->X_op = O_constant;
  464.       expressionP->X_add_number = 0;
  465.       return;
  466.     }
  467.  
  468.   /* In order to get the correct byte ordering, we must build the
  469.      number in reverse.  */
  470.   for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
  471.     {
  472.       int j;
  473.  
  474.       generic_bignum[i] = 0;
  475.       for (j = 0; j < CHARS_PER_LITTLENUM; j++)
  476.     {
  477.       if (*input_line_pointer == '\'')
  478.         {
  479.           if (input_line_pointer[1] != '\'')
  480.         break;
  481.           ++input_line_pointer;
  482.         }
  483.       generic_bignum[i] <<= 8;
  484.       generic_bignum[i] += *input_line_pointer;
  485.       ++input_line_pointer;
  486.     }
  487.  
  488.       if (i < SIZE_OF_LARGE_NUMBER - 1)
  489.     {
  490.       /* If there is more than one littlenum, left justify the
  491.              last one to make it match the earlier ones.  If there is
  492.              only one, we can just use the value directly.  */
  493.       for (; j < CHARS_PER_LITTLENUM; j++)
  494.         generic_bignum[i] <<= 8;
  495.     }
  496.  
  497.       if (*input_line_pointer == '\''
  498.       && input_line_pointer[1] != '\'')
  499.     break;
  500.     }
  501.  
  502.   if (i < 0)
  503.     {
  504.       as_bad ("Character constant too large");
  505.       i = 0;
  506.     }
  507.  
  508.   if (i > 0)
  509.     {
  510.       int c;
  511.       int j;
  512.  
  513.       c = SIZE_OF_LARGE_NUMBER - i;
  514.       for (j = 0; j < c; j++)
  515.     generic_bignum[j] = generic_bignum[i + j];
  516.       i = c;
  517.     }
  518.  
  519.   know (LITTLENUM_NUMBER_OF_BITS == 16);
  520.   if (i > 2)
  521.     {
  522.       expressionP->X_op = O_big;
  523.       expressionP->X_add_number = i;
  524.     }
  525.   else
  526.     {
  527.       expressionP->X_op = O_constant;
  528.       if (i < 2)
  529.     expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
  530.       else
  531.     expressionP->X_add_number =
  532.       (((generic_bignum[1] & LITTLENUM_MASK)
  533.         << LITTLENUM_NUMBER_OF_BITS)
  534.        | (generic_bignum[0] & LITTLENUM_MASK));
  535.     }
  536.  
  537.   /* Skip the final closing quote.  */
  538.   ++input_line_pointer;
  539. }
  540.  
  541. /* Return an expression representing the current location.  This
  542.    handles the magic symbol `.'.  */
  543.  
  544. static void
  545. current_location (expressionp)
  546.      expressionS *expressionp;
  547. {
  548.   if (now_seg == absolute_section)
  549.     {
  550.       expressionp->X_op = O_constant;
  551.       expressionp->X_add_number = abs_section_offset;
  552.     }
  553.   else
  554.     {
  555.       symbolS *symbolp;
  556.  
  557.       symbolp = symbol_new (FAKE_LABEL_NAME, now_seg,
  558.                 (valueT) frag_now_fix (),
  559.                 frag_now);
  560.       expressionp->X_op = O_symbol;
  561.       expressionp->X_add_symbol = symbolp;
  562.       expressionp->X_add_number = 0;
  563.     }
  564. }
  565.  
  566. /*
  567.  * Summary of operand().
  568.  *
  569.  * in:    Input_line_pointer points to 1st char of operand, which may
  570.  *    be a space.
  571.  *
  572.  * out:    A expressionS.
  573.  *    The operand may have been empty: in this case X_op == O_absent.
  574.  *    Input_line_pointer->(next non-blank) char after operand.
  575.  */
  576.  
  577. static segT
  578. operand (expressionP)
  579.      expressionS *expressionP;
  580. {
  581.   char c;
  582.   symbolS *symbolP;    /* points to symbol */
  583.   char *name;        /* points to name of symbol */
  584.   segT segment;
  585.  
  586.   /* All integers are regarded as unsigned unless they are negated.
  587.      This is because the only thing which cares whether a number is
  588.      unsigned is the code in emit_expr which extends constants into
  589.      bignums.  It should only sign extend negative numbers, so that
  590.      something like ``.quad 0x80000000'' is not sign extended even
  591.      though it appears negative if valueT is 32 bits.  */
  592.   expressionP->X_unsigned = 1;
  593.  
  594.   /* digits, assume it is a bignum. */
  595.  
  596.   SKIP_WHITESPACE ();        /* leading whitespace is part of operand. */
  597.   c = *input_line_pointer++;    /* input_line_pointer->past char in c. */
  598.  
  599.   switch (c)
  600.     {
  601.     case '1':
  602.     case '2':
  603.     case '3':
  604.     case '4':
  605.     case '5':
  606.     case '6':
  607.     case '7':
  608.     case '8':
  609.     case '9':
  610.       input_line_pointer--;
  611.  
  612.       integer_constant (flag_m68k_mri ? 0 : 10, expressionP);
  613.       break;
  614.  
  615.     case '0':
  616.       /* non-decimal radix */
  617.  
  618.       if (flag_m68k_mri)
  619.     {
  620.       char *s;
  621.  
  622.       /* Check for a hex constant.  */
  623.       for (s = input_line_pointer; hex_p (*s); s++)
  624.         ;
  625.       if (*s == 'h' || *s == 'H')
  626.         {
  627.           --input_line_pointer;
  628.           integer_constant (0, expressionP);
  629.           break;
  630.         }
  631.     }
  632.  
  633.       c = *input_line_pointer;
  634.       switch (c)
  635.     {
  636.     case 'o':
  637.     case 'O':
  638.     case 'q':
  639.     case 'Q':
  640.     case '8':
  641.     case '9':
  642.       if (flag_m68k_mri)
  643.         {
  644.           integer_constant (0, expressionP);
  645.           break;
  646.         }
  647.       /* Fall through.  */
  648.     default:
  649.     default_case:
  650.       if (c && strchr (FLT_CHARS, c))
  651.         {
  652.           input_line_pointer++;
  653.           floating_constant (expressionP);
  654.           expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
  655.         }
  656.       else
  657.         {
  658.           /* The string was only zero */
  659.           expressionP->X_op = O_constant;
  660.           expressionP->X_add_number = 0;
  661.         }
  662.  
  663.       break;
  664.  
  665.     case 'x':
  666.     case 'X':
  667.       if (flag_m68k_mri)
  668.         goto default_case;
  669.       input_line_pointer++;
  670.       integer_constant (16, expressionP);
  671.       break;
  672.  
  673.     case 'b':
  674.       if (LOCAL_LABELS_FB && ! flag_m68k_mri)
  675.         {
  676.           switch (input_line_pointer[1])
  677.         {
  678.         case '+':
  679.         case '-':
  680.           /* If unambiguously a difference expression, treat
  681.              it as one by indicating a label; otherwise, it's
  682.              always a binary number.  */
  683.           {
  684.             char *cp = input_line_pointer + 1;
  685.             while (strchr ("0123456789", *++cp))
  686.               ;
  687.             if (*cp == 'b' || *cp == 'f')
  688.               goto is_0b_label;
  689.           }
  690.           goto is_0b_binary;
  691.         case '0':    case '1':
  692.           /* Some of our code elsewhere does permit digits
  693.              greater than the expected base; for consistency,
  694.              do the same here.  */
  695.         case '2':    case '3':    case '4':    case '5':
  696.         case '6':    case '7':    case '8':    case '9':
  697.           goto is_0b_binary;
  698.         case 0:
  699.           goto is_0b_label;
  700.         default:
  701.           goto is_0b_label;
  702.         }
  703.         is_0b_label:
  704.           input_line_pointer--;
  705.           integer_constant (10, expressionP);
  706.           break;
  707.         is_0b_binary:
  708.           ;
  709.         }
  710.     case 'B':
  711.       input_line_pointer++;
  712.       if (flag_m68k_mri)
  713.         goto default_case;
  714.       integer_constant (2, expressionP);
  715.       break;
  716.  
  717.     case '0':
  718.     case '1':
  719.     case '2':
  720.     case '3':
  721.     case '4':
  722.     case '5':
  723.     case '6':
  724.     case '7':
  725.       integer_constant (flag_m68k_mri ? 0 : 8, expressionP);
  726.       break;
  727.  
  728.     case 'f':
  729.       if (LOCAL_LABELS_FB)
  730.         {
  731.           /* If it says "0f" and it could possibly be a floating point
  732.          number, make it one.  Otherwise, make it a local label,
  733.          and try to deal with parsing the rest later.  */
  734.           if (!input_line_pointer[1]
  735.           || (is_end_of_line[0xff & input_line_pointer[1]]))
  736.         goto is_0f_label;
  737.           {
  738.         char *cp = input_line_pointer + 1;
  739.         int r = atof_generic (&cp, ".", EXP_CHARS,
  740.                       &generic_floating_point_number);
  741.         switch (r)
  742.           {
  743.           case 0:
  744.           case ERROR_EXPONENT_OVERFLOW:
  745.             if (*cp == 'f' || *cp == 'b')
  746.               /* looks like a difference expression */
  747.               goto is_0f_label;
  748.             else
  749.               goto is_0f_float;
  750.           default:
  751.             as_fatal ("expr.c(operand): bad atof_generic return val %d",
  752.                   r);
  753.           }
  754.           }
  755.  
  756.           /* Okay, now we've sorted it out.  We resume at one of these
  757.          two labels, depending on what we've decided we're probably
  758.          looking at.  */
  759.         is_0f_label:
  760.           input_line_pointer--;
  761.           integer_constant (10, expressionP);
  762.           break;
  763.  
  764.         is_0f_float:
  765.           /* fall through */
  766.           ;
  767.         }
  768.  
  769.     case 'd':
  770.     case 'D':
  771.       if (flag_m68k_mri)
  772.         {
  773.           integer_constant (0, expressionP);
  774.           break;
  775.         }
  776.       /* Fall through.  */
  777.     case 'F':
  778.     case 'r':
  779.     case 'e':
  780.     case 'E':
  781.     case 'g':
  782.     case 'G':
  783.       input_line_pointer++;
  784.       floating_constant (expressionP);
  785.       expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
  786.       break;
  787.  
  788.     case '$':
  789.       if (LOCAL_LABELS_DOLLAR)
  790.         {
  791.           integer_constant (10, expressionP);
  792.           break;
  793.         }
  794.       else
  795.         goto default_case;
  796.     }
  797.  
  798.       break;
  799.  
  800.     case '(':
  801.     case '[':
  802.       /* didn't begin with digit & not a name */
  803.       segment = expression (expressionP);
  804.       /* Expression() will pass trailing whitespace */
  805.       if ((c == '(' && *input_line_pointer++ != ')')
  806.       || (c == '[' && *input_line_pointer++ != ']'))
  807.     {
  808.       as_bad ("Missing ')' assumed");
  809.       input_line_pointer--;
  810.     }
  811.       SKIP_WHITESPACE ();
  812.       /* here with input_line_pointer->char after "(...)" */
  813.       return segment;
  814.  
  815.     case 'E':
  816.       if (! flag_m68k_mri || *input_line_pointer != '\'')
  817.     goto de_fault;
  818.       as_bad ("EBCDIC constants are not supported");
  819.       /* Fall through.  */
  820.     case 'A':
  821.       if (! flag_m68k_mri || *input_line_pointer != '\'')
  822.     goto de_fault;
  823.       ++input_line_pointer;
  824.       /* Fall through.  */
  825.     case '\'':
  826.       if (! flag_m68k_mri)
  827.     {
  828.       /* Warning: to conform to other people's assemblers NO
  829.          ESCAPEMENT is permitted for a single quote. The next
  830.          character, parity errors and all, is taken as the value
  831.          of the operand. VERY KINKY.  */
  832.       expressionP->X_op = O_constant;
  833.       expressionP->X_add_number = *input_line_pointer++;
  834.       break;
  835.     }
  836.  
  837.       mri_char_constant (expressionP);
  838.       break;
  839.  
  840.     case '+':
  841.       (void) operand (expressionP);
  842.       break;
  843.  
  844.     case '"':
  845.       /* Double quote is the bitwise not operator in MRI mode.  */
  846.       if (! flag_m68k_mri)
  847.     goto de_fault;
  848.       /* Fall through.  */
  849.     case '!':
  850.     case '~':
  851.     case '-':
  852.       {
  853.     operand (expressionP);
  854.     if (expressionP->X_op == O_constant)
  855.       {
  856.         /* input_line_pointer -> char after operand */
  857.         if (c == '-')
  858.           {
  859.         expressionP->X_add_number = - expressionP->X_add_number;
  860.         /* Notice: '-' may overflow: no warning is given. This is
  861.            compatible with other people's assemblers. Sigh.  */
  862.         expressionP->X_unsigned = 0;
  863.           }
  864.         else if (c == '~' || c == '"')
  865.           expressionP->X_add_number = ~ expressionP->X_add_number;
  866.         else
  867.           expressionP->X_add_number = ! expressionP->X_add_number;
  868.       }
  869.     else if (expressionP->X_op != O_illegal
  870.          && expressionP->X_op != O_absent)
  871.       {
  872.         expressionP->X_add_symbol = make_expr_symbol (expressionP);
  873.         if (c == '-')
  874.           expressionP->X_op = O_uminus;
  875.         else if (c == '~' || c == '"')
  876.           expressionP->X_op = O_bit_not;
  877.         else
  878.           expressionP->X_op = O_logical_not;
  879.         expressionP->X_add_number = 0;
  880.       }
  881.     else
  882.       as_warn ("Unary operator %c ignored because bad operand follows",
  883.            c);
  884.       }
  885.       break;
  886.  
  887.     case '$':
  888.       /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
  889.          is defined.  */
  890. #ifndef DOLLAR_DOT
  891.       if (! flag_m68k_mri)
  892.     goto de_fault;
  893. #endif
  894.       if (flag_m68k_mri && hex_p (*input_line_pointer))
  895.     {
  896.       /* In MRI mode, $ is also used as the prefix for a
  897.              hexadecimal constant.  */
  898.       integer_constant (16, expressionP);
  899.       break;
  900.     }
  901.  
  902.       if (is_part_of_name (*input_line_pointer))
  903.     goto isname;
  904.  
  905.       current_location (expressionP);
  906.       break;
  907.  
  908.     case '.':
  909.       if (!is_part_of_name (*input_line_pointer))
  910.     {
  911.       current_location (expressionP);
  912.       break;
  913.     }
  914.       else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
  915.         && ! is_part_of_name (input_line_pointer[8]))
  916.            || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
  917.            && ! is_part_of_name (input_line_pointer[7])))
  918.     {
  919.       int start;
  920.  
  921.       start = (input_line_pointer[1] == 't'
  922.            || input_line_pointer[1] == 'T');
  923.       input_line_pointer += start ? 8 : 7;
  924.       SKIP_WHITESPACE ();
  925.       if (*input_line_pointer != '(')
  926.         as_bad ("syntax error in .startof. or .sizeof.");
  927.       else
  928.         {
  929.           char *buf;
  930.  
  931.           ++input_line_pointer;
  932.           SKIP_WHITESPACE ();
  933.           name = input_line_pointer;
  934.           c = get_symbol_end ();
  935.  
  936.           buf = (char *) xmalloc (strlen (name) + 10);
  937.           if (start)
  938.         sprintf (buf, ".startof.%s", name);
  939.           else
  940.         sprintf (buf, ".sizeof.%s", name);
  941.           symbolP = symbol_make (buf);
  942.           free (buf);
  943.  
  944.           expressionP->X_op = O_symbol;
  945.           expressionP->X_add_symbol = symbolP;
  946.           expressionP->X_add_number = 0;
  947.  
  948.           *input_line_pointer = c;
  949.           SKIP_WHITESPACE ();
  950.           if (*input_line_pointer != ')')
  951.         as_bad ("syntax error in .startof. or .sizeof.");
  952.           else
  953.         ++input_line_pointer;
  954.         }
  955.       break;
  956.     }
  957.       else
  958.     {
  959.       goto isname;
  960.     }
  961.     case ',':
  962.     case '\n':
  963.     case '\0':
  964.     eol:
  965.       /* can't imagine any other kind of operand */
  966.       expressionP->X_op = O_absent;
  967.       input_line_pointer--;
  968.       break;
  969.  
  970.     case '%':
  971.       if (! flag_m68k_mri)
  972.     goto de_fault;
  973.       integer_constant (2, expressionP);
  974.       break;
  975.  
  976.     case '@':
  977.       if (! flag_m68k_mri)
  978.     goto de_fault;
  979.       integer_constant (8, expressionP);
  980.       break;
  981.  
  982.     case ':':
  983.       if (! flag_m68k_mri)
  984.     goto de_fault;
  985.  
  986.       /* In MRI mode, this is a floating point constant represented
  987.          using hexadecimal digits.  */
  988.  
  989.       ++input_line_pointer;
  990.       integer_constant (16, expressionP);
  991.       break;
  992.  
  993.     case '*':
  994.       if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
  995.     goto de_fault;
  996.  
  997.       current_location (expressionP);
  998.       break;
  999.  
  1000.     default:
  1001.     de_fault:
  1002.       if (is_end_of_line[(unsigned char) c])
  1003.     goto eol;
  1004.       if (is_name_beginner (c))    /* here if did not begin with a digit */
  1005.     {
  1006.       /*
  1007.        * Identifier begins here.
  1008.        * This is kludged for speed, so code is repeated.
  1009.        */
  1010.     isname:
  1011.       name = --input_line_pointer;
  1012.       c = get_symbol_end ();
  1013.  
  1014. #ifdef TC_I960
  1015.       /* The MRI i960 assembler permits
  1016.              lda sizeof code,g13
  1017.          */
  1018.       if (flag_mri
  1019.           && (strcasecmp (name, "sizeof") == 0
  1020.           || strcasecmp (name, "startof") == 0))
  1021.         {
  1022.           int start;
  1023.           char *buf;
  1024.  
  1025.           start = (name[1] == 't'
  1026.                || name[1] == 'T');
  1027.  
  1028.           *input_line_pointer = c;
  1029.           SKIP_WHITESPACE ();
  1030.  
  1031.           name = input_line_pointer;
  1032.           c = get_symbol_end ();
  1033.  
  1034.           buf = (char *) xmalloc (strlen (name) + 10);
  1035.           if (start)
  1036.         sprintf (buf, ".startof.%s", name);
  1037.           else
  1038.         sprintf (buf, ".sizeof.%s", name);
  1039.           symbolP = symbol_make (buf);
  1040.           free (buf);
  1041.  
  1042.           expressionP->X_op = O_symbol;
  1043.           expressionP->X_add_symbol = symbolP;
  1044.           expressionP->X_add_number = 0;
  1045.  
  1046.           *input_line_pointer = c;
  1047.           SKIP_WHITESPACE ();
  1048.  
  1049.           break;
  1050.         }          
  1051. #endif
  1052.  
  1053.       symbolP = symbol_find_or_make (name);
  1054.  
  1055.       /* If we have an absolute symbol or a reg, then we know its
  1056.          value now.  */
  1057.       segment = S_GET_SEGMENT (symbolP);
  1058.       if (segment == absolute_section)
  1059.         {
  1060.           expressionP->X_op = O_constant;
  1061.           expressionP->X_add_number = S_GET_VALUE (symbolP);
  1062.         }
  1063.       else if (segment == reg_section)
  1064.         {
  1065.           expressionP->X_op = O_register;
  1066.           expressionP->X_add_number = S_GET_VALUE (symbolP);
  1067.         }
  1068.       else
  1069.         {
  1070.           expressionP->X_op = O_symbol;
  1071.           expressionP->X_add_symbol = symbolP;
  1072.           expressionP->X_add_number = 0;
  1073.         }
  1074.       *input_line_pointer = c;
  1075.     }
  1076.       else
  1077.     {
  1078.       /* Let the target try to parse it.  Success is indicated by changing
  1079.          the X_op field to something other than O_absent and pointing
  1080.          input_line_pointer passed the expression.  If it can't parse the
  1081.          expression, X_op and input_line_pointer should be unchanged.  */
  1082.       expressionP->X_op = O_absent;
  1083.       --input_line_pointer;
  1084.       md_operand (expressionP);
  1085.       if (expressionP->X_op == O_absent)
  1086.         {
  1087.           ++input_line_pointer;
  1088.           as_bad ("Bad expression");
  1089.           expressionP->X_op = O_constant;
  1090.           expressionP->X_add_number = 0;
  1091.         }
  1092.     }
  1093.       break;
  1094.     }
  1095.  
  1096.   /*
  1097.    * It is more 'efficient' to clean up the expressionS when they are created.
  1098.    * Doing it here saves lines of code.
  1099.    */
  1100.   clean_up_expression (expressionP);
  1101.   SKIP_WHITESPACE ();        /*->1st char after operand. */
  1102.   know (*input_line_pointer != ' ');
  1103.  
  1104.   /* The PA port needs this information.  */
  1105.   if (expressionP->X_add_symbol)
  1106.     expressionP->X_add_symbol->sy_used = 1;
  1107.  
  1108.   switch (expressionP->X_op)
  1109.     {
  1110.     default:
  1111.       return absolute_section;
  1112.     case O_symbol:
  1113.       return S_GET_SEGMENT (expressionP->X_add_symbol);
  1114.     case O_register:
  1115.       return reg_section;
  1116.     }
  1117. }                /* operand() */
  1118.  
  1119. /* Internal. Simplify a struct expression for use by expr() */
  1120.  
  1121. /*
  1122.  * In:    address of a expressionS.
  1123.  *    The X_op field of the expressionS may only take certain values.
  1124.  *    Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
  1125.  * Out:    expressionS may have been modified:
  1126.  *    'foo-foo' symbol references cancelled to 0,
  1127.  *        which changes X_op from O_subtract to O_constant.
  1128.  *    Unused fields zeroed to help expr().
  1129.  */
  1130.  
  1131. static void
  1132. clean_up_expression (expressionP)
  1133.      expressionS *expressionP;
  1134. {
  1135.   switch (expressionP->X_op)
  1136.     {
  1137.     case O_illegal:
  1138.     case O_absent:
  1139.       expressionP->X_add_number = 0;
  1140.       /* Fall through.  */
  1141.     case O_big:
  1142.     case O_constant:
  1143.     case O_register:
  1144.       expressionP->X_add_symbol = NULL;
  1145.       /* Fall through.  */
  1146.     case O_symbol:
  1147.     case O_uminus:
  1148.     case O_bit_not:
  1149.       expressionP->X_op_symbol = NULL;
  1150.       break;
  1151.     case O_subtract:
  1152.       if (expressionP->X_op_symbol == expressionP->X_add_symbol
  1153.       || ((expressionP->X_op_symbol->sy_frag
  1154.            == expressionP->X_add_symbol->sy_frag)
  1155.           && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
  1156.           && (S_GET_VALUE (expressionP->X_op_symbol)
  1157.           == S_GET_VALUE (expressionP->X_add_symbol))))
  1158.     {
  1159.       addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
  1160.                - S_GET_VALUE (expressionP->X_op_symbol));
  1161.  
  1162.       expressionP->X_op = O_constant;
  1163.       expressionP->X_add_symbol = NULL;
  1164.       expressionP->X_op_symbol = NULL;
  1165.       expressionP->X_add_number += diff;
  1166.     }
  1167.       break;
  1168.     default:
  1169.       break;
  1170.     }
  1171. }
  1172.  
  1173. /* Expression parser. */
  1174.  
  1175. /*
  1176.  * We allow an empty expression, and just assume (absolute,0) silently.
  1177.  * Unary operators and parenthetical expressions are treated as operands.
  1178.  * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
  1179.  *
  1180.  * We used to do a aho/ullman shift-reduce parser, but the logic got so
  1181.  * warped that I flushed it and wrote a recursive-descent parser instead.
  1182.  * Now things are stable, would anybody like to write a fast parser?
  1183.  * Most expressions are either register (which does not even reach here)
  1184.  * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
  1185.  * So I guess it doesn't really matter how inefficient more complex expressions
  1186.  * are parsed.
  1187.  *
  1188.  * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
  1189.  * Also, we have consumed any leading or trailing spaces (operand does that)
  1190.  * and done all intervening operators.
  1191.  *
  1192.  * This returns the segment of the result, which will be
  1193.  * absolute_section or the segment of a symbol.
  1194.  */
  1195.  
  1196. #undef __
  1197. #define __ O_illegal
  1198.  
  1199. static operatorT op_encoding[256] =
  1200. {                /* maps ASCII->operators */
  1201.  
  1202.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1203.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1204.  
  1205.   __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
  1206.   __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
  1207.   __, __, __, __, __, __, __, __,
  1208.   __, __, __, __, O_lt, __, O_gt, __,
  1209.   __, __, __, __, __, __, __, __,
  1210.   __, __, __, __, __, __, __, __,
  1211.   __, __, __, __, __, __, __, __,
  1212.   __, __, __, __, __, __, O_bit_exclusive_or, __,
  1213.   __, __, __, __, __, __, __, __,
  1214.   __, __, __, __, __, __, __, __,
  1215.   __, __, __, __, __, __, __, __,
  1216.   __, __, __, __, O_bit_inclusive_or, __, __, __,
  1217.  
  1218.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1219.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1220.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1221.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1222.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1223.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1224.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1225.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
  1226. };
  1227.  
  1228.  
  1229. /*
  1230.  *    Rank    Examples
  1231.  *    0    operand, (expression)
  1232.  *    1    ||
  1233.  *    2    &&
  1234.  *    3    = <> < <= >= >
  1235.  *    4    + -
  1236.  *    5    used for * / % in MRI mode
  1237.  *    6    & ^ ! |
  1238.  *    7    * / % << >>
  1239.  *    8    unary - unary ~
  1240.  */
  1241. static operator_rankT op_rank[] =
  1242. {
  1243.   0,    /* O_illegal */
  1244.   0,    /* O_absent */
  1245.   0,    /* O_constant */
  1246.   0,    /* O_symbol */
  1247.   0,    /* O_symbol_rva */
  1248.   0,    /* O_register */
  1249.   0,    /* O_bit */
  1250.   8,    /* O_uminus */
  1251.   8,    /* O_bit_not */
  1252.   8,    /* O_logical_not */
  1253.   7,    /* O_multiply */
  1254.   7,    /* O_divide */
  1255.   7,    /* O_modulus */
  1256.   7,    /* O_left_shift */
  1257.   7,    /* O_right_shift */
  1258.   6,    /* O_bit_inclusive_or */
  1259.   6,    /* O_bit_or_not */
  1260.   6,    /* O_bit_exclusive_or */
  1261.   6,    /* O_bit_and */
  1262.   4,    /* O_add */
  1263.   4,    /* O_subtract */
  1264.   3,    /* O_eq */
  1265.   3,    /* O_ne */
  1266.   3,    /* O_lt */
  1267.   3,    /* O_le */
  1268.   3,    /* O_ge */
  1269.   3,    /* O_gt */
  1270.   2,    /* O_logical_and */
  1271.   1    /* O_logical_or */
  1272. };
  1273.  
  1274. /* Initialize the expression parser.  */
  1275.  
  1276. void
  1277. expr_begin ()
  1278. {
  1279.   /* In MRI mode for the m68k, multiplication and division have lower
  1280.      precedence than the bit wise operators.  */
  1281.   if (flag_m68k_mri)
  1282.     {
  1283.       op_rank[O_multiply] = 5;
  1284.       op_rank[O_divide] = 5;
  1285.       op_rank[O_modulus] = 5;
  1286.       op_encoding['"'] = O_bit_not;
  1287.     }
  1288.  
  1289.   /* Verify that X_op field is wide enough.  */
  1290.   {
  1291.     expressionS e;
  1292.     e.X_op = O_max;
  1293.     assert (e.X_op == O_max);
  1294.   }
  1295. }
  1296.  
  1297. /* Return the encoding for the operator at INPUT_LINE_POINTER.
  1298.    Advance INPUT_LINE_POINTER to the last character in the operator
  1299.    (i.e., don't change it for a single character operator).  */
  1300.  
  1301. static inline operatorT
  1302. operator ()
  1303. {
  1304.   int c;
  1305.   operatorT ret;
  1306.  
  1307.   c = *input_line_pointer;
  1308.  
  1309.   switch (c)
  1310.     {
  1311.     default:
  1312.       return op_encoding[c];
  1313.  
  1314.     case '<':
  1315.       switch (input_line_pointer[1])
  1316.     {
  1317.     default:
  1318.       return op_encoding[c];
  1319.     case '<':
  1320.       ret = O_left_shift;
  1321.       break;
  1322.     case '>':
  1323.       ret = O_ne;
  1324.       break;
  1325.     case '=':
  1326.       ret = O_le;
  1327.       break;
  1328.     }
  1329.       ++input_line_pointer;
  1330.       return ret;
  1331.  
  1332.     case '>':
  1333.       switch (input_line_pointer[1])
  1334.     {
  1335.     default:
  1336.       return op_encoding[c];
  1337.     case '>':
  1338.       ret = O_right_shift;
  1339.       break;
  1340.     case '=':
  1341.       ret = O_ge;
  1342.       break;
  1343.     }
  1344.       ++input_line_pointer;
  1345.       return ret;
  1346.  
  1347.     case '!':
  1348.       /* We accept !! as equivalent to ^ for MRI compatibility.  */
  1349.       if (input_line_pointer[1] != '!')
  1350.     {
  1351.       if (flag_m68k_mri)
  1352.         return O_bit_inclusive_or;
  1353.       return op_encoding[c];
  1354.     }
  1355.       ++input_line_pointer;
  1356.       return O_bit_exclusive_or;
  1357.  
  1358.     case '|':
  1359.       if (input_line_pointer[1] != '|')
  1360.     return op_encoding[c];
  1361.  
  1362.       ++input_line_pointer;
  1363.       return O_logical_or;
  1364.  
  1365.     case '&':
  1366.       if (input_line_pointer[1] != '&')
  1367.     return op_encoding[c];
  1368.  
  1369.       ++input_line_pointer;
  1370.       return O_logical_and;
  1371.     }
  1372.  
  1373.   /*NOTREACHED*/
  1374. }    
  1375.  
  1376. /* Parse an expression.  */
  1377.  
  1378. segT
  1379. expr (rank, resultP)
  1380.      operator_rankT rank;    /* Larger # is higher rank. */
  1381.      expressionS *resultP;    /* Deliver result here. */
  1382. {
  1383.   segT retval;
  1384.   expressionS right;
  1385.   operatorT op_left;
  1386.   operatorT op_right;
  1387.  
  1388.   know (rank >= 0);
  1389.  
  1390.   retval = operand (resultP);
  1391.  
  1392.   know (*input_line_pointer != ' ');    /* Operand() gobbles spaces. */
  1393.  
  1394.   op_left = operator ();
  1395.   while (op_left != O_illegal && op_rank[(int) op_left] > rank)
  1396.     {
  1397.       segT rightseg;
  1398.  
  1399.       input_line_pointer++;    /*->after 1st character of operator. */
  1400.  
  1401.       rightseg = expr (op_rank[(int) op_left], &right);
  1402.       if (right.X_op == O_absent)
  1403.     {
  1404.       as_warn ("missing operand; zero assumed");
  1405.       right.X_op = O_constant;
  1406.       right.X_add_number = 0;
  1407.       right.X_add_symbol = NULL;
  1408.       right.X_op_symbol = NULL;
  1409.     }
  1410.  
  1411.       know (*input_line_pointer != ' ');
  1412.  
  1413.       if (retval == undefined_section)
  1414.     {
  1415.       if (SEG_NORMAL (rightseg))
  1416.         retval = rightseg;
  1417.     }
  1418.       else if (! SEG_NORMAL (retval))
  1419.     retval = rightseg;
  1420.       else if (SEG_NORMAL (rightseg)
  1421.            && retval != rightseg
  1422. #ifdef DIFF_EXPR_OK
  1423.            && op_left != O_subtract
  1424. #endif
  1425.            )
  1426.     as_bad ("operation combines symbols in different segments");
  1427.  
  1428.       op_right = operator ();
  1429.  
  1430.       know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]);
  1431.       know ((int) op_left >= (int) O_multiply
  1432.         && (int) op_left <= (int) O_logical_or);
  1433.  
  1434.       /* input_line_pointer->after right-hand quantity. */
  1435.       /* left-hand quantity in resultP */
  1436.       /* right-hand quantity in right. */
  1437.       /* operator in op_left. */
  1438.  
  1439.       if (resultP->X_op == O_big)
  1440.     {
  1441.       as_warn ("left operand is a %s; integer 0 assumed",
  1442.            resultP->X_add_number > 0 ? "bignum" : "float");
  1443.       resultP->X_op = O_constant;
  1444.       resultP->X_add_number = 0;
  1445.       resultP->X_add_symbol = NULL;
  1446.       resultP->X_op_symbol = NULL;
  1447.     }
  1448.       if (right.X_op == O_big)
  1449.     {
  1450.       as_warn ("right operand is a %s; integer 0 assumed",
  1451.            right.X_add_number > 0 ? "bignum" : "float");
  1452.       right.X_op = O_constant;
  1453.       right.X_add_number = 0;
  1454.       right.X_add_symbol = NULL;
  1455.       right.X_op_symbol = NULL;
  1456.     }
  1457.  
  1458.       /* Optimize common cases.  */
  1459.       if (op_left == O_add && right.X_op == O_constant)
  1460.     {
  1461.       /* X + constant.  */
  1462.       resultP->X_add_number += right.X_add_number;
  1463.     }
  1464.       /* This case comes up in PIC code.  */
  1465.       else if (op_left == O_subtract
  1466.            && right.X_op == O_symbol
  1467.            && resultP->X_op == O_symbol
  1468.            && (right.X_add_symbol->sy_frag
  1469.            == resultP->X_add_symbol->sy_frag)
  1470.            && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
  1471.  
  1472.     {
  1473.       resultP->X_add_number += right.X_add_number;
  1474.       resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
  1475.                     - S_GET_VALUE (right.X_add_symbol));
  1476.       resultP->X_op = O_constant;
  1477.       resultP->X_add_symbol = 0;
  1478.     }
  1479.       else if (op_left == O_subtract && right.X_op == O_constant)
  1480.     {
  1481.       /* X - constant.  */
  1482.       resultP->X_add_number -= right.X_add_number;
  1483.     }
  1484.       else if (op_left == O_add && resultP->X_op == O_constant)
  1485.     {
  1486.       /* Constant + X.  */
  1487.       resultP->X_op = right.X_op;
  1488.       resultP->X_add_symbol = right.X_add_symbol;
  1489.       resultP->X_op_symbol = right.X_op_symbol;
  1490.       resultP->X_add_number += right.X_add_number;
  1491.       retval = rightseg;
  1492.     }
  1493.       else if (resultP->X_op == O_constant && right.X_op == O_constant)
  1494.     {
  1495.       /* Constant OP constant.  */
  1496.       offsetT v = right.X_add_number;
  1497.       if (v == 0 && (op_left == O_divide || op_left == O_modulus))
  1498.         {
  1499.           as_warn ("division by zero");
  1500.           v = 1;
  1501.         }
  1502.       switch (op_left)
  1503.         {
  1504.         default:            abort ();
  1505.         case O_multiply:        resultP->X_add_number *= v; break;
  1506.         case O_divide:        resultP->X_add_number /= v; break;
  1507.         case O_modulus:        resultP->X_add_number %= v; break;
  1508.         case O_left_shift:        resultP->X_add_number <<= v; break;
  1509.         case O_right_shift:        resultP->X_add_number >>= v; break;
  1510.         case O_bit_inclusive_or:    resultP->X_add_number |= v; break;
  1511.         case O_bit_or_not:        resultP->X_add_number |= ~v; break;
  1512.         case O_bit_exclusive_or:    resultP->X_add_number ^= v; break;
  1513.         case O_bit_and:        resultP->X_add_number &= v; break;
  1514.         case O_add:            resultP->X_add_number += v; break;
  1515.         case O_subtract:        resultP->X_add_number -= v; break;
  1516.         case O_eq:
  1517.           resultP->X_add_number =
  1518.         resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
  1519.           break;
  1520.         case O_ne:
  1521.           resultP->X_add_number =
  1522.         resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
  1523.           break;
  1524.         case O_lt:
  1525.           resultP->X_add_number =
  1526.         resultP->X_add_number <  v ? ~ (offsetT) 0 : 0;
  1527.           break;
  1528.         case O_le:
  1529.           resultP->X_add_number =
  1530.         resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
  1531.           break;
  1532.         case O_ge:
  1533.           resultP->X_add_number =
  1534.         resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
  1535.           break;
  1536.         case O_gt:
  1537.           resultP->X_add_number =
  1538.         resultP->X_add_number >  v ? ~ (offsetT) 0 : 0;
  1539.           break;
  1540.         case O_logical_and:
  1541.           resultP->X_add_number = resultP->X_add_number && v;
  1542.           break;
  1543.         case O_logical_or:
  1544.           resultP->X_add_number = resultP->X_add_number || v;
  1545.           break;
  1546.         }
  1547.     }
  1548.       else if (resultP->X_op == O_symbol
  1549.            && right.X_op == O_symbol
  1550.            && (op_left == O_add
  1551.            || op_left == O_subtract
  1552.            || (resultP->X_add_number == 0
  1553.                && right.X_add_number == 0)))
  1554.     {
  1555.       /* Symbol OP symbol.  */
  1556.       resultP->X_op = op_left;
  1557.       resultP->X_op_symbol = right.X_add_symbol;
  1558.       if (op_left == O_add)
  1559.         resultP->X_add_number += right.X_add_number;
  1560.       else if (op_left == O_subtract)
  1561.         resultP->X_add_number -= right.X_add_number;
  1562.     }
  1563.       else
  1564.     {
  1565.       /* The general case.  */
  1566.       resultP->X_add_symbol = make_expr_symbol (resultP);
  1567.       resultP->X_op_symbol = make_expr_symbol (&right);
  1568.       resultP->X_op = op_left;
  1569.       resultP->X_add_number = 0;
  1570.       resultP->X_unsigned = 1;
  1571.     }
  1572.  
  1573.       op_left = op_right;
  1574.     }                /* While next operator is >= this rank. */
  1575.  
  1576.   /* The PA port needs this information.  */
  1577.   if (resultP->X_add_symbol)
  1578.     resultP->X_add_symbol->sy_used = 1;
  1579.  
  1580.   return resultP->X_op == O_constant ? absolute_section : retval;
  1581. }
  1582.  
  1583. /*
  1584.  *            get_symbol_end()
  1585.  *
  1586.  * This lives here because it belongs equally in expr.c & read.c.
  1587.  * Expr.c is just a branch office read.c anyway, and putting it
  1588.  * here lessens the crowd at read.c.
  1589.  *
  1590.  * Assume input_line_pointer is at start of symbol name.
  1591.  * Advance input_line_pointer past symbol name.
  1592.  * Turn that character into a '\0', returning its former value.
  1593.  * This allows a string compare (RMS wants symbol names to be strings)
  1594.  * of the symbol name.
  1595.  * There will always be a char following symbol name, because all good
  1596.  * lines end in end-of-line.
  1597.  */
  1598. char
  1599. get_symbol_end ()
  1600. {
  1601.   char c;
  1602.  
  1603.   /* We accept \001 in a name in case this is being called with a
  1604.      constructed string.  */
  1605.   while (is_part_of_name (c = *input_line_pointer++)
  1606.      || c == '\001')
  1607.     ;
  1608.   *--input_line_pointer = 0;
  1609.   return (c);
  1610. }
  1611.  
  1612.  
  1613. unsigned int
  1614. get_single_number ()
  1615. {
  1616.   expressionS exp;
  1617.   operand (&exp);
  1618.   return exp.X_add_number;
  1619.  
  1620. }
  1621.  
  1622. /* end of expr.c */
  1623.